package com.SouthernWall_404.AgeDust.Common.Network;

import com.SouthernWall_404.AgeDust.Common.AgeDust;
import com.SouthernWall_404.AgeDust.Common.Network.C2S.CharacterScreenClientPacket;
import com.SouthernWall_404.AgeDust.Common.Network.S2C.CharacterScreenServerPacket;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.network.NetworkRegistry;
import net.minecraftforge.network.PacketDistributor;
import net.minecraftforge.network.simple.SimpleChannel;

public class ModChannels {

    private static final String PROTOCOL_VERSION = "1";
    private static int id=0;


    public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(
            new ResourceLocation(AgeDust.MODID, "main"),
            () -> PROTOCOL_VERSION,
            PROTOCOL_VERSION::equals,
            PROTOCOL_VERSION::equals
    );

    public static void register()
    {
        INSTANCE.registerMessage(
                id++,
                CharacterScreenServerPacket.class,      // 客户端发送给服务端的数据包类
                CharacterScreenServerPacket::encode,    // 序列化方法
                CharacterScreenServerPacket::new,       // 反序列化构造函数
                CharacterScreenServerPacket::handle     // 服务端处理逻辑
        );
        INSTANCE.registerMessage(
                id++,
                CharacterScreenClientPacket.class,      // 客户端发送给服务端的数据包类
                CharacterScreenClientPacket::encode,    // 序列化方法
                CharacterScreenClientPacket::new,       // 反序列化构造函数
                CharacterScreenClientPacket::handle     // 服务端处理逻辑
        );


    }


    public static <MSG> void sendToServer(MSG message)
    {
        INSTANCE.sendToServer(message);
    }
    public static <MSG> void sendToClient(MSG message, Player player)
    {
        INSTANCE.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), message);}

}
